/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Table, TableBody, TableRow, CCell, TableHead, Text, useTheme, Button,
} from "customize-easy-ui-component";
import {DirectLink, Link as RouterLink,} from "../../../routing/Link";
import {ReportViewProps,} from "../../common/base";
import {末尾链接, 落款单位地址,} from "../../common/rarelyVary";
import {config设备, 报告设备详情上mMs,} from "./repView";
import {useRepMenuDirItems, } from "../../hook/useMainRepUrlOr";
import {setupItemAreaRoute} from "./orcIspConfig";
import queryString from "query-string";
import {FormatOriginal, } from "./FormatOriginal";
import RoutingContext from "../../../routing/RoutingContext";
import {useContext} from "react";
import {useItemsMapOmni} from "../../common/omni";
import {Column_Setting} from "../../common/useFormatOmni";
import {useOfficialOmni, } from "../../common/useOfficialOmni";
import {UnqualifiedIspTable} from "../../common/general";
import {ReportFirstPageHeadJd} from "../../park/rarelyVary";
import {注意事项mMs, 编制核准人员} from "./rarelyVary";
import {首页概况mMs} from "./viewFirst";

export const ReportView: React.FunctionComponent<ReportViewProps> = ({
repId,  source: orc,  verId,rep,
}) => {
    const qs= queryString.parse(window.location.search);
    const formatOriginal =qs && !!qs.original;      //改成  格式化版原始记录
    const { history } = useContext(RoutingContext);
    const Component=formatOriginal? FormatOriginal : OfficialReport;
    return (<>
        <Component  source={orc} verId={verId} repId={repId} rep={rep}/>
        <div css={{margin: '0.5rem', "@media print": {display: 'none'} }}>
            <Button intent="danger" variant="outline"
                  onPress={async () => {
                      qs.original =formatOriginal? '' : '1';
                      history.location.search = queryString.stringify(qs);
                      const toUrl= history.createHref(history.location);
                      history.push(toUrl);
                  }}>{formatOriginal? '正式报告':'格式化版原始记录'}
            </Button>
        </div>
    </>);
}

const config报告 : Column_Setting[]=[{n:'ic',x:'类别',m:true}, {n:null,x:'结论'},{n:'M',x:'备注',m:true}];
const JumpTags=[{name:'设备概况',ha:'Survey'},{name:'12.1监控要求的硬件配置',ha:'12.1'},
                {name:'12.11起重综合误差试验',ha:'12.11'} ];
const OfficialReport: React.FunctionComponent<ReportViewProps> = ({
    repId,   source: orc,  verId,rep,
}) => {
    const qs= queryString.parse(window.location.search);
    const printing =qs && !!qs.print;
    const theme= useTheme();
    const [rootMenu]=useRepMenuDirItems(JumpTags, [],`/reportView/${rep?.modeltype}/ver/${rep?.modelversion}/${rep?.id}`);
    //【特殊部分】orc?._Oitems: 动态，用户定制的；
    const impressionismAs =React.useMemo(() => {
        return setupItemAreaRoute({rep,orc, theme});
    }, [verId, repId,rep, orc?._Oitems, theme]);
    const {renderIspContent} =useOfficialOmni({orc,ItemArs:impressionismAs?.Item, rep, config:config报告, });
    const [mapNoTag]=useItemsMapOmni({ ItemArs:impressionismAs?.Item, notCheckNo:false});
  return (
    <React.Fragment>
      {rootMenu}
       <div css={{"@media not print": {marginTop: '1rem', marginBottom: '1rem'}}}>
            <div css={{"@media print": {height: '100vh'}}}>
                {ReportFirstPageHeadJd({theme, rep, mbbm: 'JB/QB-1143-1-2020'})}
                <div css={{
                    "@media print": {
                        display: 'flex',
                        flexDirection: 'column',
                        justifyContent: 'space-between',
                        height: 'calc(100vh - 8.5rem)',
                    }
                }}>
                    <Text variant="h3" css={{textAlign: 'center', "@media print": {fontSize: theme.fontSizes[5], marginTop: '1.5rem',},}}>
                      起重机械安全监控管理系统监督检验报告
                    </Text>
                    {首页概况mMs({theme, orc, rep,noOid:false})}
                    <div css={{textAlign: 'center', "@media print": {pageBreakAfter: 'always', pageBreakInside: 'avoid'}}}>
                        {落款单位地址}
                    </div>
                </div>
            </div>
           {注意事项mMs({rep,
               comply:'依据《起重机械安装改造重大修理监督检验规则》（TSGQ7016-2016)（及第1号修改单）适用于起重机械安全监控管理系统检验。本报告为整机检验报告的一部分'
           }) }
           <DirectLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Instrument`}>
               <div css={{
                   display: 'flex',
                   flexDirection: 'column',
                   justifyContent: 'center',
                   "@media print": {
                       pageBreakBefore: 'always',
                   },
               }}>
                   <Text variant="h4">一、设备概况</Text>
               </div>
               <div css={{display: 'flex', justifyContent: 'space-between'}}>
                   <Text></Text>
                   <Text>报告编号：{rep.isp.no}</Text>
               </div>
           </DirectLink>
           {报告设备详情上mMs({orc, rep,config:config设备, spIdx:9,ispan:5,gybz:false,fixed: ["12.6%","8%","31%","11%","8%","%"],
                         xnFixed: ["4.8%","13.5%","29%","9%","13.3%","10%","%"] })}
           {编制核准人员({orc,rep,})}
           <Text css={{"@media print": {fontSize: '0.75rem'}}}>
             注：本表设备信息是指安装安全监控管理系统的起重机的参数。监控系统的生产单位名称、型号、编号填“/”表示监控系统未出独立合格证。
           </Text>
           <div css={{"@media print": {paddingBottom: '13.3rem', pageBreakInside: 'avoid'}}}>
                <div css={{display: 'flex', justifyContent: 'space-between',minHeight:'1rem'}}>
                    <Text></Text><Text></Text>
                </div>
            </div>
           <Table fixed={ ["4%","10%","5%","4%","5%","%","5%","8%","11%"] }
                  css={ {borderCollapse: 'collapse',"@media print": {marginTop: '-13.3rem'}} }   tight  miniw={800}>
               <TableHead>
                   <DirectLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/ALL`}>
                       <TableRow>
                           <CCell><Text css={{fontSize:'0.75rem'}}>序号</Text></CCell>
                           <CCell colSpan={5}>检验项目及内容和要求</CCell>
                           <CCell><Text css={{fontSize:'0.7rem'}}>检验类别</Text></CCell>
                           <CCell>检验结果</CCell>
                           <CCell>备注</CCell>
                       </TableRow>
                   </DirectLink>
               </TableHead>
               <TableBody>
                   {renderIspContent}
               </TableBody>
           </Table>
           <UnqualifiedIspTable rep={rep} orc={orc} mapNoTag={mapNoTag} printing={printing} titles={['序号','项目编号','检验不合格项目内容','复检结果','复检日期']}
                                label={<Text variant="h4">检验不合格项目内容</Text>}/>
        </div>
        <div>
            <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Instrument#Instrument`}>
                <Text variant="h4" css={{"@media print": {display: 'none'}}}>主要测量设备性能检查</Text>
            </RouterLink>
            <RouterLink href={`/report/${rep?.modeltype}/ver/${verId}/${repId}/Witness#Witness`}>
                <Text variant="h4" css={{"@media print": {display: 'none'}}}>见证材料 、备注</Text>
            </RouterLink>
        </div>
      {末尾链接({template:rep?.modeltype,verId, repId: repId||''})}
    </React.Fragment>
  );
}
